﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Microsoft.SharePoint;

namespace AWS.SLAM
{
    /// <summary>
    /// 
    /// </summary>
    public static class ComponentService
    {
        private static IComponentFactory _componentFactory;
        private static string _defaultFactoryQualifiedTypeName = "AWS.SLAM.StructureMap.ComponentFactory, AWS.SLAM.StructureMap, Version=1.0.1.0, Culture=neutral, PublicKeyToken=ba006d3bc480a2e8";

        /// <summary>
        /// 
        /// </summary>
        static ComponentService()
        {
            if (ConfigurationManager.AppSettings == null || ConfigurationManager.AppSettings["AWS.SLAM.ComponentService.ComponentFactoryQualifiedTypeName"] == null)
            {
                _componentFactory = Activator.CreateInstance(Type.GetType(_defaultFactoryQualifiedTypeName)) as IComponentFactory;
            }
            else
            {
                _componentFactory = Activator.CreateInstance(Type.GetType(ConfigurationManager.AppSettings["AWS.SLAM.ComponentService.ComponentFactoryQualifiedTypeName"])) as IComponentFactory;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static void ClearInstances()
        {
            _componentFactory.ResetContainer();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T GetInstance<T>()
        {   
            return _componentFactory.GetInstance<T>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="contentType"></param>
        /// <returns></returns>
        public static T GetInstance<T>(string siteCollection, string site, string typeName)
        {
            return _componentFactory.GetInstance<T>(siteCollection, site, typeName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public static T GetInstance<T>(string siteCollection, string site, string id, string typeName)
        {
            return _componentFactory.GetInstance<T>(siteCollection, site, id, typeName);
        }
    }
}
